package cn.ekuma.epos.crm;

import java.util.Date;
import java.util.Properties;

import com.openbravo.bean.crm.CustomerContact;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSlider;
import java.awt.Dimension;

public class CustomerContactEditor extends AbstractChildDAOJEditor<CustomerContact,String> {
	private JTextField txtContactTime;
	private JTextField txtContactType;
	private JTextField txtCustomerUser;
	private JTextField txtUser;
	private JTextField txtFollowUpTime;
	private JTextField txtResult;
	private JTextArea txtMemo;
	private JButton btnNewButton_1;
	private JButton btnNewButton;
	private JComboBox comboBox_1;
	private JSlider cmbPriority;

	public CustomerContactEditor(DirtyManager dirty) {
		super(dirty);
		setPreferredSize(new Dimension(430, 326));
		initComponents();
		txtContactTime.getDocument().addDocumentListener(dirty);
		txtContactType.getDocument().addDocumentListener(dirty);
	    txtCustomerUser.getDocument().addDocumentListener(dirty);
		txtUser.getDocument().addDocumentListener(dirty);
		txtFollowUpTime.getDocument().addDocumentListener(dirty);
	    txtResult.getDocument().addDocumentListener(dirty);
	    txtMemo.getDocument().addDocumentListener(dirty);
	    cmbPriority.addChangeListener(dirty);
	    
	    comboBox_1.addItem("来电");
	    comboBox_1.addItem("去电");
	    comboBox_1.addItem("拜访");
	    comboBox_1.addItem("招待");
	    comboBox_1.addItem("电邮");
	    comboBox_1.addItem("投诉");  
	}
	private void initComponents() {
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel(AppLocal.getIntString("label.contactTime"));
		lblNewLabel.setBounds(10, 10, 54, 15);
		add(lblNewLabel);
		
		txtContactTime = new JTextField();
		txtContactTime.setBounds(115, 7, 247, 21);
		add(txtContactTime);
		txtContactTime.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel(AppLocal.getIntString("label.priority"));
		lblNewLabel_1.setBounds(10, 40, 54, 15);
		add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel(AppLocal.getIntString("label.contactType"));
		lblNewLabel_2.setBounds(10, 69, 54, 15);
		add(lblNewLabel_2);
		
		txtContactType = new JTextField();
		txtContactType.setBounds(115, 66, 154, 21);
		add(txtContactType);
		txtContactType.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel(AppLocal.getIntString("label.customerUser"));
		lblNewLabel_3.setBounds(226, 95, 84, 15);
		add(lblNewLabel_3);
		
		txtCustomerUser = new JTextField();
		txtCustomerUser.setBounds(314, 92, 101, 21);
		add(txtCustomerUser);
		txtCustomerUser.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel(AppLocal.getIntString("label.user"));
		lblNewLabel_4.setBounds(10, 95, 101, 15);
		add(lblNewLabel_4);
		
		txtUser = new JTextField();
		txtUser.setBounds(115, 92, 101, 21);
		add(txtUser);
		txtUser.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel(AppLocal.getIntString("label.followUpTime"));
		lblNewLabel_5.setBounds(10, 289, 54, 15);
		add(lblNewLabel_5);
		
		txtFollowUpTime = new JTextField();
		txtFollowUpTime.setBounds(115, 286, 247, 21);
		add(txtFollowUpTime);
		txtFollowUpTime.setColumns(10);
		
	   txtMemo = new JTextArea();
		txtMemo.setBorder(new TitledBorder(null, AppLocal.getIntString("label.notes"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		txtMemo.setBounds(10, 125, 405, 126);
		add(txtMemo);
		
		JLabel lblNewLabel_6 = new JLabel(AppLocal.getIntString("label.result"));
		lblNewLabel_6.setBounds(10, 261, 54, 15);
		add(lblNewLabel_6);
		
		txtResult = new JTextField();
		txtResult.setBounds(115, 258, 300, 21);
		add(txtResult);
		txtResult.setColumns(10);
		
		btnNewButton = new JButton("");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDateStartActionPerformed(null);
			}
		});
		btnNewButton.setIcon(new ImageIcon(CustomerContactEditor.class.getResource("/com/openbravo/images/date.png")));
		btnNewButton.setBounds(372, 6, 43, 23);
		add(btnNewButton);
		
	    btnNewButton_1 = new JButton("");
	    btnNewButton_1.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {
	    		btnDateEndActionPerformed(null);
	    	}
	    });
		btnNewButton_1.setIcon(new ImageIcon(CustomerContactEditor.class.getResource("/com/openbravo/images/date.png")));
		btnNewButton_1.setBounds(372, 285, 43, 23);
		add(btnNewButton_1);
		
		comboBox_1 = new JComboBox();
		comboBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtContactType.setText((String) comboBox_1.getSelectedItem());
			}
		});
		
		comboBox_1.setBounds(279, 66, 136, 21);
		add(comboBox_1);
		
		  cmbPriority = new JSlider();
		    cmbPriority.setMaximum(15);
		    cmbPriority.setBounds(115, 36, 279, 23);
		    add(cmbPriority);
	}

	
	 private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
	        Date date;
	        try {
	            date = (Date) Formats.TIMESTAMP.parseValue(txtContactTime.getText());
	        } catch (BasicException e) {
	            date = null;
	        }
	        date = JCalendarDialog.showCalendarTimeHours(this, date);
	        if (date != null) {
	        	txtContactTime.setText(Formats.TIMESTAMP.formatValue(date));
	        }
	}//GEN-LAST:event_btnDateStartActionPerformed

	    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
	        Date date;
	        try {
	            date = (Date) Formats.TIMESTAMP.parseValue(txtFollowUpTime.getText());
	        } catch (BasicException e) {
	            date = null;
	        }
	        date = JCalendarDialog.showCalendarTimeHours(this, date);
	        if (date != null) {
	        	txtFollowUpTime.setText(Formats.TIMESTAMP.formatValue(date));
	        }
	}//GEN-LAST:event_btnDateEndActionPerformed
	
	@Override
	public String getTabName() {
		return AppLocal.getIntString("label.CustomerContact");
	}

	@Override
	public void refresh() {
	}

	@Override
	public CustomerContact createNew(Properties prop) {
		CustomerContact newObj=new CustomerContact();
		newObj.setContactTime(new Date());
		if(parentNode!=null)
            newObj.setCustomerId(parentNode.getParentKey().getKey());
		return newObj;
	}

	@Override
	public void rebuild(CustomerContact obj) throws BasicException {
		obj.setContactTime((Date) Formats.TIMESTAMP.parseValue(txtContactTime.getText()));
		obj.setContactType(txtContactType.getText());
		obj.setCustomerUser(txtCustomerUser.getText());
		obj.setUser(txtUser.getText());
		obj.setFollowUpTime((Date) Formats.TIMESTAMP.parseValue(txtFollowUpTime.getText()));
		obj.setResult(txtResult.getText());
		obj.setMemo(txtMemo.getText());
		obj.setPriority(cmbPriority.getValue());
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtContactTime.setText(Formats.TIMESTAMP.formatValue(obj.getContactTime()));
		txtContactType.setText(obj.getContactType());
	    txtCustomerUser.setText(obj.getCustomerUser());
		txtUser.setText(obj.getUser());
		txtFollowUpTime.setText(Formats.TIMESTAMP.formatValue(obj.getFollowUpTime()));
	    txtResult.setText(obj.getResult());
	    txtMemo.setText(obj.getMemo());
	    cmbPriority.setValue(obj.getPriority());
	    
	    txtContactTime.setEditable(editAble);
		txtContactType.setEditable(editAble);
	    txtCustomerUser.setEditable(editAble);
		txtUser.setEditable(editAble);
		txtFollowUpTime.setEditable(editAble);
	    txtResult.setEditable(editAble);
	    txtMemo.setEditable(editAble);
	    btnNewButton.setEnabled(editAble);
	    btnNewButton_1.setEnabled(editAble);
	    cmbPriority.setEnabled(editAble);
	    comboBox_1.setEditable(editAble);
	}

	@Override
	public Class getEditClass() {
		return CustomerContact.class;
	}
}
